import React, { useEffect, useState } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';

export async function addTx(tx: any) {
  let txs = [];
  console.log("start addTX")
  try {
    const s = await AsyncStorage.getItem('@txs');
    if (s) {
      txs = JSON.parse(s);
    }
  } catch (e) {
    console.log('@txs get', e);
  }

  txs.push(tx);

  try {
    await AsyncStorage.setItem('@txs', JSON.stringify(txs))
  } catch (e) {
    console.log('@txs set', e);
  }
};

export async function saveTX(tx:any) {
  await AsyncStorage.setItem('@txs', JSON.stringify(tx));
}

//type代表增删改
export async function saveTXone(tx:any,type:string) {
  const txs = await getTxs();
  if(type === "add")//增
  {
    for(let i=0;i<txs.length;i++)
    {
      if(txs[i].hash == tx.hash)
      {
        break;
      }
      if(i === (txs.length - 1))
      {
        txs.push(tx);
      }
    }
  }
  else if(type === "del")//删
  {
    for(let i=0;i<txs.length;i++)
    {
      if(txs[i].hash == tx.hash)
      {
        txs.splice(i,1)
        break;
      }
    }
  }
  else if(type === "modify")//改
  {
    for(let i=0;i<txs.length;i++)
    {
      if(txs[i].hash == tx.hash)
      {
        txs[i] = tx;
        break;
      }
    }
  }

  await AsyncStorage.setItem('@txs', JSON.stringify(txs));


}

export async function getTxFromSaveByhash(hash:string) {
  if(!hash)
  {
    return null;
  }
  const txs = await getTxs();
  for(let i=0;i<txs.length;i++)
  {
    if(txs[i].hash == hash)
    {
      return txs[i];
    }
  }
  return null;
}

export async function popTx(hash:any) {
  const txs = await getTxs();
  for(let i=0;i<txs.length;i++)
  {
    if(txs[i].hash==hash)
    {
      txs.splice(i,1);
      break;
    }
  }

  try {
    await AsyncStorage.setItem('@txs', JSON.stringify(txs))
  } catch (e) {
    console.log('@txs set', e);
  }
};

export async function getTxs() {
  let txs = [];

  try {
    const s = await AsyncStorage.getItem('@txs');
    if (s) {
      txs = JSON.parse(s);
    }
  } catch (e) {
    console.log('@txs get', e);
  }

  return txs;
};

export async function getTxs2() {
  let txs = [];

  try {
    const s = await AsyncStorage.getItem('@txs');
    if (s) {
      txs = JSON.parse(s);
    }
  } catch (e) {
    console.log('@txs get', e);
  }
  return txs;
};

export async function getFirstPendingTx() {
  const txs = await getTxs();
  return txs.length ? txs[0] : null;
};



//红包部分
export async function addRedEnvelopeTx(tx: any,num:string,amount:string,type:string,tokenName:string,tokenAddress:string) {
  let txs = [];
  console.log("start addRedEnvelopeTx")
  try {
    const s = await AsyncStorage.getItem('@RedEnvelopetxs');
    if (s) {
      txs = JSON.parse(s);
    }
  } catch (e) {
    console.log('@RedEnvelopetxs get', e);
  }

  txs.push(tx);

  try {
    await AsyncStorage.setItem('@RedEnvelopetxs', JSON.stringify(txs))
    await AsyncStorage.setItem('@RedEnvelopeNum', num)
    await AsyncStorage.setItem('@RedEnvelopeAmount', amount)
    await AsyncStorage.setItem('@RedEnvelopeHash', tx.hash)
    await AsyncStorage.setItem('@RedEnvelopeType', type)
    await AsyncStorage.setItem('@RedEnvelopeTokenName', tokenName)
    await AsyncStorage.setItem('@RedEnvelopeTokenAddress', tokenAddress)
  } catch (e) {
    console.log('@txs set', e);
  }
};

export async function getRedEnvelopeTxs() {
  let txs = [];

  try {
    const s = await AsyncStorage.getItem('@RedEnvelopetxs');
    if (s) {
      txs = JSON.parse(s);
    }
  } catch (e) {
    console.log('@RedEnvelopetxs get', e);
  }

  return txs;
};


//首页红包领取信息
export async function addRedEnvelopeGet(tx:string) {
  let txs = [];
  console.log("start addRedEnvelopeGet")
  try {
    const s = await AsyncStorage.getItem('@addRedEnvelopeGet');
    if (s) {
      txs = JSON.parse(s);
    }
  } catch (e) {
    console.log('@addRedEnvelopeGet get', e);
  }

  txs.push(tx);

  try {
    await AsyncStorage.setItem('@addRedEnvelopeGet', JSON.stringify(txs))
  } catch (e) {
    console.log('@addRedEnvelopeGet set', e);
  }
};


export async function getRedEnvelopeGet() {
  let txs = [];

  try {
    const s = await AsyncStorage.getItem('@addRedEnvelopeGet');
    if (s) {
      txs = JSON.parse(s);
    }
  } catch (e) {
    console.log('@addRedEnvelopeGet get', e);
  }

  return txs;
};
